package com.serviceservlet;

import java.awt.List;
import java.io.IOException;
import java.util.HashMap;
import java.util.HashSet;
import java.util.Iterator;
import java.util.Map;
import java.util.Map.Entry;

import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import com.commonfunction.CommFunction;
import com.databasefunction.AllDBFunction;
import com.databasefunction.DatabaseConstants;

/**
 * Servlet implementation class Getnameservlet
 */

public class Getnameservlet extends HttpServlet {
	private static final long serialVersionUID = 1L;
       
    /**
     * @see HttpServlet#HttpServlet()
     */
    public Getnameservlet() {
        super();
        // TODO Auto-generated constructor stub
    }

	/**
	 * @see HttpServlet#doGet(HttpServletRequest request, HttpServletResponse response)
	 */
	protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
		// TODO Auto-generated method stub
		CommFunction commFunction = new CommFunction();
		AllDBFunction allDBFunction = new AllDBFunction();
		System.out.println(request.getParameter("methodetocall")+ " Get called");
		if(request.getParameter("methodetocall").equalsIgnoreCase("getdealername")){
		String values = "";
		List list = allDBFunction.getCustmer(request.getParameter("name"), request.getParameter("role"));
		for (int i = 0; i < list.getItemCount(); i++) {
			values += list.getItem(i)+"#";
		}
		response.getWriter().write(values);
		}else if(request.getParameter("methodetocall").equalsIgnoreCase("getaddress")){
			String address=commFunction.getAddress(request.getParameter("name"));
			response.getWriter().write(address);
			
		}else if(request.getParameter("methodetocall").equalsIgnoreCase("getusersname")){
			String values = "";
			List list = allDBFunction.getuserlist(request.getParameter("name"));
			for (int i = 0; i < list.getItemCount(); i++) {
				values += list.getItem(i)+"#";
			}
			response.getWriter().write(values);
		}else if (request.getParameter("methodetocall").equalsIgnoreCase("getsearch")) {
			List list = null;
			String values = "";
			String searchBy = request.getParameter("searchby");
			String searchtext = request.getParameter("name");
			
			if(searchBy.equalsIgnoreCase("city") || searchBy.equalsIgnoreCase("district")){
				list = allDBFunction.getcityordist(searchtext,searchBy);
				list = refinList(list);
			}else{
				list = allDBFunction.getCustmer(searchtext, searchBy);
				/*HashMap datamap = (HashMap)hashMap.get("dealerData");
				Iterator iterator = datamap.entrySet().iterator();
				while (iterator.hasNext()) {
					Map.Entry entry = (Entry) iterator.next();
					System.out.println("Keys:"+entry.getKey());
					
				}*/
			}
			
			for (int i = 0; i < list.getItemCount(); i++) {
				values += list.getItem(i)+"#";
			}
			response.getWriter().write(values);

			}else if(request.getParameter("methodetocall").equalsIgnoreCase("showorhide")){
				String showorHide = request.getParameter("showorhide");
				String barcode = request.getParameter("barcode");
				allDBFunction.changeBusinessHidden(barcode, showorHide);
				
			}else if(request.getParameter("methodetocall").equalsIgnoreCase("getStrData")){
				String rstno = request.getParameter("rstno");
				HashMap hashMap = allDBFunction.getRSTData(rstno);
				
				
			}
		
	}

	/**
	 * @see HttpServlet#doPost(HttpServletRequest request, HttpServletResponse response)
	 */
	protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
		// TODO Auto-generated method stub
	}
	
	public List refinList(List list){
		HashSet hashSet = new HashSet();
		List list2 = new List();
		for (int i = 0; i < list.getItemCount(); i++) {
			if (!hashSet.contains(list.getItem(i))) {
				list2.add(list.getItem(i));
				hashSet.add(list.getItem(i));
			}
		}
		//System.out.println(hashSet);
		/*
		for (int i = 0; i < list2.getItemCount(); i++) {
			System.out.println(list2.getItem(i)+" ");
		}
		*/
		return list2;
	}

	
}
